<template>
	<div class='login container'>
		<Header></Header>
		<section>
			<div class='login-tel'>
				<input type="text" v-model="userTel" placeholder="请输入手机号" pattern="[0-9]*">
			</div>
			<div class='login-code'>
				<input type="text" placeholder="请输入短信验证码" pattern="[0-9]*" v-model="userCode">
				<button @click="sendCode" :disabled="disabled">{{codeMsg}}</button>
			</div>
			<div class='login-btn' @click="login">登 录</div>
			<div class='tab'>
				<span @click='goUserLogin'>密码登录</span>
				<span @click="$router.push('/register')">快速注册</span>
			</div>
		</section>
		<Tabbar></Tabbar>
	</div>
</template>

<script>
import Tabbar from '@/components/common/Tabbar.vue'
import Header from './Header'
import { Toast } from 'mint-ui'
import http from '@/common/api/request'
import {mapMutations} from 'vuex'
import { USER_LOGIN } from '@/store/modules/mutation-type'
export default{
	components:{
		Header,
		Tabbar
	},
	data(){
		return {
			code:'',						//保存短信验证码
			userCode:'',				//绑定短信验证码输入框
			disabled:false,
			userTel:'',					//保存用户电话	
			codeNum:60,					//倒计时
			codeMsg:'获取短信验证码',
			relus:{
				// 手机号验证
				userTel:{
					rule:/^1[3-9]\d{9}$/,
					msg:'手机号不能为空,并且是11位数据'
				},
			}
		}
	},
	methods:{
		...mapMutations(['userLogin']),
		// 登录
		login(){
			if(this.code==this.userCode){
				//证明用户输入的短信验证码是正确的
				http.$axios({
					url:'/api/addUser',
					method:'POST',
					data:{
						phone:this.userTel
					}
				}).then(res=>{
					if( !res.success ) return;
					this.userLogin(res.data)
					// 跳转我的主页
					this.$router.push('my')
				})
				//发送请求
			}else{
				Toast('验证码错误')
			}
		},

		// 发送验证码
		sendCode(){
			// 验证手机号
			if(!this.validate('userTel')) return 
			// 禁用按钮
			this.disabled=true

			//请求短信验证码接口
			http.$axios({
				url:'/api/code',
				method:'POST',
				data:{
					phone:this.userTel
				}
			}).then(res=>{
				console.log(res)
				if( res.success ){
					this.code = res.data;
				}
			})

			// 设置倒计时
			let timer=setInterval(()=>{
				this.codeNum--
				this.codeMsg=`重新发送(${this.codeNum})`
			},1000)
			// 设置停止
			setTimeout(()=>{
				clearInterval( timer );
				this.codeNum = 6;
				this.disabled = false;
				this.codeMsg = '获取短信验证码';
			},60000)


		},
		// 页面跳转
		goUserLogin(){
			this.$router.push('/userLogin');
		},
		// 手机验证
		validate( key ){
			let bool=true
			if(!this.relus[key].rule.test(this[key])){
				Toast(this.relus[key].msg)
				bool=false
			}
			return bool
		}
	}
}
</script>

<style scoped lang='scss'>
section{
	display: flex;
	flex-direction: column;
	align-items: center;
	background-color:#f5f5f5;
	div{
		margin:0.266666rem 0;
		width: 8.933333rem;
		height: 1.173333rem;
	}
	input{
		font-size: 0.2rem;
		box-sizing: border-box;
		padding:0 0.266666rem;
		line-height: 1.173333rem;
		background-color: #FFFFFF;
		border:1px solid #ccc;
		border-radius: 6px;
	}
	.login-tel{
		margin-top:0.8rem;
		input{
			width: 8.933333rem;
		}
	}
	.login-code{
		display: flex;
		input{
			flex:1;
		}
		button{
			padding:0 0.533333rem;
			line-height: 1.173333rem;
			color:#fff;
			background-color: #b0352f;
			border:0;
			border-radius: 6px;
			font-size:0.2rem;
		}
	}
	.login-btn{
		line-height: 44px;
		color:#fff;
		text-align: center;
		background-color: #b0352f;
		border-radius: 6px;
		font-size:0.5rem;
	}
	.tab{
		display: flex;
		justify-content: space-between;
		font-size:0.373333rem;
	}
}
</style>
